//线程的局部存储: 全局变量定义一次，每个线程都拥有一个

#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;

__thread int g_val = 100; //__thread是一个编译选项, 编译的时候就会把线程控制块中的空间开辟出来 --- 拷贝
//到线程局部存储空间中!

void* threadRoutine(void* args)
{
    string name = static_cast<const char* >(args);
    while(true)
    {
        cout << "I am new thread" << ", thread name: " << name << ", g_val:" << g_val 
            << ", &g_val: " << &g_val << endl << endl;
        g_val++;
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void*)"thread-1");
    while(true)
    {
        cout << "I am main thread" <<  ", g_val:" << g_val 
            << ", &g_val: " << &g_val << endl << endl;
        sleep(1);
    }

    pthread_join(tid, nullptr); 
    return 0;
}